package com.cos.shop.service.impl;

import com.cos.shop.common.PageHelper;
import com.cos.shop.common.ResponseResult;
import com.cos.shop.dao.ProductDao;
import com.cos.shop.dao.impl.ProductDaoImpl;
import com.cos.shop.entity.Product;
import com.cos.shop.service.ProductServices;

import java.sql.SQLException;
import java.util.List;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: ProductServicesImpl
 * Author: saber
 * Date:2022/9/17 11:02
 */
public class ProductServicesImpl implements ProductServices {

    private ProductDao pd = new ProductDaoImpl();

    @Override
    public ResponseResult getProductByPage(int tid,int page) {

        try {
            if (tid>0){
                int pageSize = 8;

                Long totalSize = pd.getProductCoun(tid);

                PageHelper<Product> ph = new PageHelper<>(page,pageSize,totalSize.intValue());

                List<Product> list = pd.getProductByPage(tid,ph.getStartRow(),pageSize);

                ph.setList(list);

                return ResponseResult.createSuccess(ph);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResponseResult.createFail("抱歉，暂时没有该商品");
    }

    @Override
    public ResponseResult getProductById( int pid) {

        try {
            if (pid>0) {
                Product product = pd.getProductByPid(pid);

                return ResponseResult.createSuccess(product);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return ResponseResult.createFail("抱歉，暂时没有该商品的详细信息");
    }
}
